//bange_sprite.cpp
//Copyright (C) 2010-2011 Oscar (.teri) Triano

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <iostream>
#include <bange_sprite.hpp>

//Rethink about rewrite object as simpleobject and this class as object (simpleobject + body)

bange::sprite::sprite(cpSpace *space, lua_State *vm): bange::body(space), bange::simplesprite(vm){}

bange::sprite::~sprite(){
	std::cout << "bange::sprite::~destructor: " << this << std::endl;
}


bool bange::sprite::NewIndex(lua_State *vm){
	//sprite, key, value
	char key[64] = {0};
	strncpy(key, lua_tostring(vm, 2), 64);
	if (this->bange::body::NewIndex(vm)){
		return true;}
	if (strcmp(key, "x") == 0 && lua_isnumber(vm, 3)){
		this->SetBodyX(lua_tonumber(vm, 3));
		return true;
	}
	else if (strcmp(key, "y") == 0 && lua_isnumber(vm, 3)){
		this->SetBodyY(lua_tonumber(vm, 3));
		return true;
	}
	//This avoids the simplesprite's x and y
	if (this->bange::simplesprite::NewIndex(vm)){
		return true;}
	return false;
}

bool bange::sprite::Index(lua_State *vm){
	//sprite, key
	char key[64] = {0};
	strncpy(key, lua_tostring(vm, 2), 64);
	if (this->bange::body::Index(vm)){
		return true;}
	if (strcmp(key, "x") == 0){
		lua_pushnumber(vm, this->GetBodyX());
		return true;
	}
	else if (strcmp(key, "y") == 0){
		lua_pushnumber(vm, this->GetBodyY());
		return true;
	}
	//Same that above
	if (this->bange::simplesprite::Index(vm)){
		return true;}
	return false;
}

void bange::sprite::Process(float time){
	this->bange::animation::Process(time);
	cpVect pos = cpBodyGetPos(this->thebody);
	this->SetPosition(pos.x, pos.y);
	
	//Plus update sprite position
}
